package com.zbkj.crmeb.store.controller;

import java.util.Set;

import javax.validation.constraints.NotEmpty;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crmeb.core.common.CommonResult;
import com.zbkj.crmeb.store.model.StoreProduct;
import com.zbkj.crmeb.store.model.StoreSeason;
import com.zbkj.crmeb.store.request.StoreSeasonRequest;
import com.zbkj.crmeb.store.service.StoreProductService;
import com.zbkj.crmeb.store.service.StoreSeasonService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 季节管理
 * @Description: 
 * @author daigb
 * @date 2021-04-13
 */
@RestController
@RequestMapping("api/admin/store/season")
@Api(tags = "季节管理")
public class StoreSeasonController {
    @Autowired
    StoreSeasonService storeSeasonService;
    @Autowired
    StoreProductService storeProductService;

    /**
     * 列表
     * @Description:  
     * @author daigb 
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "列表")
    @GetMapping("/list")
    public CommonResult<Object> listEntity() {
        return CommonResult.success(storeSeasonService.selectEntityList());
    }
    
    /**
     * 分页查询
     * @Description:  
     * @author daigb 
     * @param @param storeBrandRequest
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "分页")
    @PostMapping("/page")
    public CommonResult<Object> pageEntity(@RequestBody StoreSeasonRequest request) {
        return CommonResult.success(storeSeasonService.pageEntity(request));
    }
    
    /**
     * 通过id查询
     * @Description:  
     * @author daigb 
     * @param @param id
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "通过id获取")
    @GetMapping("/{id}")
    public CommonResult<Object> getEntity(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id) {
        return CommonResult.success(storeSeasonService.getEntityById(id));
    }
    
    /**
     * 新增
     * @Description:  
     * @author daigb 
     * @param @param storeBrandDTO
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public CommonResult<Object> save(@RequestBody StoreSeason request) {
    	StoreSeason storeSeason = storeSeasonService.getOne(Wrappers.<StoreSeason>lambdaQuery().eq(StoreSeason :: getName, request.getName()));
        if(null != storeSeason) {
            return CommonResult.validateFailed("该季节已存在");
        }
        return 0 < storeSeasonService.saveEntity(request) ? CommonResult.success() : CommonResult.failed();
    }
    
    @ApiOperation(value = "修改")
    @PutMapping("/update")
    public CommonResult<Object> update(@RequestBody StoreSeason request) {
        if(null == request.getId()) {
            return CommonResult.validateFailed("ID不能为空");
        }
        if(null == request.getName()) {
            return CommonResult.validateFailed("名字不能为空");
        }
    	StoreSeason storeSeason = storeSeasonService.getOne(Wrappers.<StoreSeason>lambdaQuery().eq(StoreSeason :: getName, request.getName()));
        if(null != storeSeason) {
            return CommonResult.validateFailed("该季节已存在");
        }
        
        return CommonResult.success(storeSeasonService.updateEntity(request));
    }
    
    /**
     * 根据id删除
     * @Description:  
     * @author daigb 
     * @param @param id
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/{id}")
    public CommonResult<Object> del(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id) {
    	int count = storeProductService.count(Wrappers.<StoreProduct>lambdaQuery().eq(StoreProduct :: getSeasonCode, storeSeasonService.getById(id).getName()));
        if(0 < count) {
            return CommonResult.validateFailed("该季节有商品, 无法删除");
        }
        
        return CommonResult.success(storeSeasonService.removeById(id));
    }
    
    /**
     * 批量删除
     * @Description:  
     * @author daigb 
     * @param @param ids
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping("/remove")
    public CommonResult<Object> delBatch(@NotEmpty(message = "id不能为空") @RequestBody Set<Integer> ids) {
        return CommonResult.success(storeSeasonService.removeByIds(ids));
    }

}
